package ws.test.lq;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.lang.reflect.*;
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


public class DBType {

	public static final String Lookup = "java:comp/env/jdbc/cuczz";
	
	public Connection getConnFormPool(){
		
		Connection conn = null;
		try{
			Context context = new InitialContext();
			DataSource ds = (DataSource)context.lookup(Lookup);
			conn = ds.getConnection();
		}catch(Exception e){
			//Out.print(e.getMessage());
			System.out.print(e.getMessage());
		}
		return conn;	
	}
	
	public Connection getConn() {
		Connection conn = null;
		try{
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
			String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CUCZZ"; 
			String user="cuczz"; 
			String password="cuczz"; 
			conn= DriverManager.getConnection(url,user,password); 
		}catch(Exception e){
			System.out.print("db error="+e.getMessage());
		}
		return conn;
	}
	
	
	public static void main(String args[])
	{
		DBType db = new DBType();
		
		String sql="select Tint,Tlong,Tdouble,TdateTime,Tchar from TempTable where Tint=1";
		String attrs[]={"IntType","LongType","DoubleType","DateType","StringType"};
		String columns[]={"Tint","Tlong","Tdouble","Tdatetime","Tchar"};
		Connection conn = null;
		Statement stmt = null;
		try{
			conn = db.getConnFormPool();
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			ResultSetMetaData rsmd = rs.getMetaData();
			rs.next(); //移动游标
			try{
				Class stuClass = Class.forName("ws.test.lq.ParamClass");
				ParamClass stu = new ParamClass();
				for(int i=1; i<=rsmd.getColumnCount();i++)
				{
					
					System.out.println("StringType ColumnClassName="+rsmd.getColumnClassName(i));
					//System.out.println("StringType ColumnName="+rsmd.getColumnName(i));
				 	Class type = null;
					Object value = null;
					if(rsmd.getColumnClassName(i).equals("java.lang.Integer"))
					{
						type = int.class;
						System.out.println("rs="+rs.getInt(i));
						value = rs.getInt(i);
					}
					else if(rsmd.getColumnClassName(i).equals("java.lang.Long"))
					{
						type = long.class;
						System.out.println("rs="+rs.getLong(i));
						value = rs.getLong(i);
					}
					else if(rsmd.getColumnClassName(i).equals("java.lang.Float"))
					{
						type = double.class;
						value = rs.getDouble(i);
					}
					else if(rsmd.getColumnClassName(i).equals("java.sql.Timestamp"))
					{
						type = Date.class;
						value = rs.getDate(i);
					}
					else if(rsmd.getColumnClassName(i).equals("java.lang.String"))
					{
						type = String.class;
						value = rs.getString(i);
					}
					else
					{
						System.out.print("not surport this type");
					}
					Class argTypes[] ={type};  //函数参数类型
					
					Method m = stuClass.getMethod("set"+attrs[i-1], argTypes); //得到该函数
					Object values[] ={value}; //要调用的函数参数的值
					System.out.println(type);
					System.out.println(value);
					m.invoke(stu,values);
				}
				System.out.print(stu);
				
			}catch(Exception e)
			{
				System.out.print("relection error="+e.getMessage());
			}
		}catch(SQLException ex)
		{
			System.out.print(ex.getMessage());
			
		}finally
		{
			try{
				if(stmt != null)
				{
					stmt.close();
				}
				if(conn != null)
				{
					conn.close();					
				}
			}catch(SQLException eg)
			{
				
				System.out.print(eg.getMessage());
			}
			
		}
		
		
	}
	
}
